package org.dromara.product.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.product.domain.bo.KyStockOutRelationsBo;
import org.dromara.product.domain.vo.KyStockOutRelationsVo;
import org.dromara.product.domain.KyStockOutRelations;
import org.dromara.product.mapper.KyStockOutRelationsMapper;
import org.dromara.product.service.IKyStockOutRelationsService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 出库关系Service业务层处理
 *
 * @author kuyu
 * @date 2023-12-29
 */
@RequiredArgsConstructor
@Service
public class KyStockOutRelationsServiceImpl implements IKyStockOutRelationsService {

    private final KyStockOutRelationsMapper baseMapper;

    /**
     * 查询出库关系
     */
    @Override
    public KyStockOutRelationsVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询出库关系列表
     */
    @Override
    public TableDataInfo<KyStockOutRelationsVo> queryPageList(KyStockOutRelationsBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<KyStockOutRelations> lqw = buildQueryWrapper(bo);
        Page<KyStockOutRelationsVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询出库关系列表
     */
    @Override
    public List<KyStockOutRelationsVo> queryList(KyStockOutRelationsBo bo) {
        LambdaQueryWrapper<KyStockOutRelations> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<KyStockOutRelations> buildQueryWrapper(KyStockOutRelationsBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<KyStockOutRelations> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getOutStockNo()), KyStockOutRelations::getOutStockNo, bo.getOutStockNo());
        lqw.eq(bo.getOutRecordId() != null, KyStockOutRelations::getOutRecordId, bo.getOutRecordId());
        lqw.eq(bo.getProductId() != null, KyStockOutRelations::getProductId, bo.getProductId());
        lqw.eq(bo.getNumber() != null, KyStockOutRelations::getNumber, bo.getNumber());
        lqw.eq(bo.getBuforeNum() != null, KyStockOutRelations::getBuforeNum, bo.getBuforeNum());
        lqw.eq(bo.getPrice() != null, KyStockOutRelations::getPrice, bo.getPrice());
        lqw.eq(bo.getTotalMoney() != null, KyStockOutRelations::getTotalMoney, bo.getTotalMoney());
        lqw.eq(StringUtils.isNotBlank(bo.getBathInNo()), KyStockOutRelations::getBathInNo, bo.getBathInNo());
        lqw.eq(bo.getStatus() != null, KyStockOutRelations::getStatus, bo.getStatus());
        lqw.eq(bo.getApproveLevel() != null, KyStockOutRelations::getApproveLevel, bo.getApproveLevel());
        lqw.eq(bo.getPrintNum() != null, KyStockOutRelations::getPrintNum, bo.getPrintNum());
        lqw.eq(StringUtils.isNotBlank(bo.getProductInfo()), KyStockOutRelations::getProductInfo, bo.getProductInfo());
        lqw.eq(bo.getStockInId() != null, KyStockOutRelations::getStockInId, bo.getStockInId());
        lqw.eq(StringUtils.isNotBlank(bo.getImages()), KyStockOutRelations::getImages, bo.getImages());
        lqw.eq(bo.getOutTypeId() != null, KyStockOutRelations::getOutTypeId, bo.getOutTypeId());
        lqw.eq(bo.getWarehouseId() != null, KyStockOutRelations::getWarehouseId, bo.getWarehouseId());
        lqw.eq(bo.getZoneId() != null, KyStockOutRelations::getZoneId, bo.getZoneId());
        lqw.eq(StringUtils.isNotBlank(bo.getMark()), KyStockOutRelations::getMark, bo.getMark());
        lqw.eq(bo.getCustomerId() != null, KyStockOutRelations::getCustomerId, bo.getCustomerId());
        lqw.eq(bo.getMid() != null, KyStockOutRelations::getMid, bo.getMid());
        lqw.eq(StringUtils.isNotBlank(bo.getItem()), KyStockOutRelations::getItem, bo.getItem());
        lqw.eq(StringUtils.isNotBlank(bo.getCustom()), KyStockOutRelations::getCustom, bo.getCustom());
        lqw.eq(bo.getDeletedAt() != null, KyStockOutRelations::getDeletedAt, bo.getDeletedAt());
        lqw.eq(bo.getFromWarehouseId() != null, KyStockOutRelations::getFromWarehouseId, bo.getFromWarehouseId());
        lqw.eq(bo.getFromZoneId() != null, KyStockOutRelations::getFromZoneId, bo.getFromZoneId());
        return lqw;
    }

    /**
     * 新增出库关系
     */
    @Override
    public Boolean insertByBo(KyStockOutRelationsBo bo) {
        KyStockOutRelations add = MapstructUtils.convert(bo, KyStockOutRelations.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改出库关系
     */
    @Override
    public Boolean updateByBo(KyStockOutRelationsBo bo) {
        KyStockOutRelations update = MapstructUtils.convert(bo, KyStockOutRelations.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(KyStockOutRelations entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除出库关系
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
